/**
 * Created by chengming on 18/4/13.
 * version 1.0
 */
"use strict";
const schedule = require('node-schedule');
const logger = require('./conf/logger');
const iconv = require("iconv-lite");
const request = require('request-promise');
const ip = require('./util/ip');
const sleep = require('await-sleep');
const report = require('./exception/ErrorReport');
const server = require('./conf/server');

const ArticleHrefAdapter = require('./adapter/ArticleHrefAdapter');
const SpecialAdapter = require('./adapter/SpecialAdapter');
const JsonAdapter = require('./adapter/JsonAdapter');
const JsonpAdapter = require('./adapter/JsonpAdapter');
const type = require('./conf/argv')();
/*
await new SpecialAdapter({
	"pageSite": "http://money.163.com/",
	"realSite": "http://money.163.com/special/00251G8F/news_json.js",
	"pageTitle": "网易财经",
	"deltaTime": "30000",
	"followLink": 0,
	"titleRule": "",
	"contentRule": "",
	"foreign": 0,
	"linkRule": "list#title#url",
	"headers": '',
	"cates": "加密货币",
	"priority": "50",
	"linkUniqueBy": 1,
	"encoding": "gbk",
	"ctype": 2,
	"channel": '公告'
}, false).execute();
*/


(async function () {

  // while (true) {

  let echo = {}
  try {

    echo.code = 200;

    let page = {
      id: 576,
      pageSite: "http://www.mohurd.gov.cn/wjfb/index.html",
      realSite: "",
      sitemd5: "",
      rangeMd5: "",
      pageTitle: "住建部",
      deltaTime: 10000,
      followLink: 0,
      titleRule: "",
      contentRule: "",
      linkRule: "table tbody tr:nth-child(2) table tbody tr:nth-child(2) table tbody tr td:nth-child(2) a",
      ctype: 1,
      encoding: "UTF-8",
      foreign: 0,
      linkUniqueBy: 1,
      createtime: "2018-08-31 10:46:13",
      channel: "政策发布",
      status: 2,
      headers: "",
      cates: "公告",
      group: "1",
      extra: "",
      priority: 50,
      updateAt: "2018-08-16 13:25:01.0",
      skipError: 0,
      asSpecial: 0,
      detailEncoding: "UTF-8"
    }
    //pageTitle: 法拉第未来
    // realSite: https://www.faradayfuturecn.com/api/press-room/postlist?region=CN&limit=6&start=0&year=
    // pageSite: https://www.faradayfuturecn.com/cn/press-room/
    // channel: 新闻室
    // deltaTime: 10000
    // cates: 文章
    // priority: 50
    // followLink: 0
    // headers: 
    // linkRule: pages.template.props#description#http://baidu.com
    // titleRule: 
    // contentRule: 
    // ctype: 1
    // linkUniqueBy: 1
    // encoding: UTF-8
    // foreign: 0
    // extra: 
    // detailEncoding: 
    // asSpecial: 0
    // let page = {
    //   id: 576,
    //   pageSite: 'https://www.fmprc.gov.cn/web/wjbz_673089/zyhd_673091/',
    //   realSite: '',
    //   sitemd5: "",
    //   rangeMd5: "",
    //   pageTitle: "新闻室",
    //   deltaTime: 10000,
    //   followLink: 0,
    //   titleRule: "",
    //   contentRule: "",
    //   linkRule: ".rebox_news ul li a",
    //   ctype: 1,
    //   encoding: "UTF-8",
    //   foreign: 0,
    //   linkUniqueBy: 1,
    //   createtime: "2018-08-31 10:46:13",
    //   channel: "政策发布",
    //   status: 2,
    //   headers: "",
    //   cates: "公告",
    //   group: "1",
    //   extra: "",
    //   priority: 50,
    //   updateAt: "2018-08-16 13:25:01.0",
    //   skipError: 0,
    //   asSpecial: 0,
    //   detailEncoding: "UTF-8"
    // }
    echo.data = page;

    if (echo.code != 200) {
      await sleep(1000);
    } else {
      if (echo.data) {
        logger.debug("从种子队列中获取任务:" + JSON.stringify(echo));
        let page = echo.data;
        if (1 == page.asSpecial || SpecialAdapter.specialSites.indexOf(page.pageSite.trim()) != -1) {
          //处理特殊情况
          //   await new SpecialAdapter(page, true).execute();
        } else if (1 == page.ctype) {
          //内容为普通html
          await new ArticleHrefAdapter(page, true).execute();
        } else if (2 == page.ctype) {
          //内容为json
          await new JsonAdapter(page, true).execute();
        } else if (3 == page.ctype) {
          //内容为jsonp
          await new JsonpAdapter(page, true).execute();
        }
      } else {
        console.log("-------------echo.data null")
      }
    }

  } catch (e) {
    logger.error(e);
    logger.error('在处理seed->' + JSON.stringify(echo) + '出错了,5秒后重试');
    if (echo && echo.code == 200) {
      if (e.message.indexOf('"type":"Buffer"') > -1) {
        report(echo.data.pageSite, echo.data.pageTitle, '', '获取种子站点异常,异常原因:' + e.message + "\n error: " + iconv.decode(e.error, 'utf-8').toString() + " \n错误堆栈:" + e.stack);
      } else {
        report(echo.data.pageSite, echo.data.pageTitle, '', '获取种子站点异常,异常原因:' + e.message + " \n错误堆栈:" + e.stack);
      }
    }
    await sleep(10000);
  }
  // }

})();
